import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import '../../../../utils/util.dart';
import '../../tabbar/index.dart';
import '../view.dart';
import 'controller.dart';

abstract class ITabRefreshPage<T extends ITabRefreshController>
    extends IRefreshView<T> with TabBarMixin {
  const ITabRefreshPage({Key? key}) : super(key: key);

  @protected
  Widget indexPageBuilder(BuildContext context, int index,
      {ScrollPhysics? physics});
  @protected
  Widget buildTabBar(BuildContext context, bool isFloat) {
    return indicatorTabBar(
        controller: controller.state.tabCtrl!,
        titles: controller.state.tabTitles);
  }

  @protected
  Widget buildTabBarView(
    BuildContext context, {
    required List<Widget> children,
  }) {
    return TabBarView(controller: controller.state.tabCtrl, children: children);
  }

  Widget buildUpdateTabBar(BuildContext context, bool isFloat) {
    return GetBuilder<T>(
      id: controller.state.updateTabbarId,
      builder: (controller) {
        if (controller.state.tabCtrl == null) {
          return const SizedBox();
        }
        return buildTabBar(context, isFloat);
      },
    );
  }

  ///tabbarview 处理刷新 懒加载逻辑
  @protected
  Widget buildUpdateTabBarView(BuildContext context, {ScrollPhysics? physics}) {
    return GetBuilder<T>(
      id: controller.state.updateTabBarViewId,
      builder: (controller) {
        final state = controller.state;
        if (state.tabCtrl == null) {
          return const SizedBox.shrink();
        }
        return buildTabBarView(context,
            children: List.generate(state.tabTitles!.length, (index) {
              return GetBuilder<T>(
                id: state.getUpdateLayoutPageId(index),
                builder: (controller) {
                  if (state.tabIndex.value == index) {
                    return GetBuilder<T>(
                      id: state.updatePageId,
                      builder: (controller) {
                        return KeepAlivePage(
                          child: indexPageBuilder(context, index,
                              physics: physics),
                        );
                      },
                    );
                  }

                  return const SizedBox.shrink();
                },
              );
            }));
      },
    );
  }

  @override
  Widget buildRefreshContainer(BuildContext context) {
    return buildRefreshBody(context);
  }
}

class ITabBarView extends StatefulWidget {
  const ITabBarView({
    super.key,
    required this.pageBuilder,
    this.controller,
    this.physics,
    this.dragStartBehavior = DragStartBehavior.start,
    this.viewportFraction = 1.0,
    this.clipBehavior = Clip.hardEdge,
  });
  final IndexedWidgetBuilder pageBuilder;
  final TabController? controller;
  final ScrollPhysics? physics;
  final DragStartBehavior dragStartBehavior;
  final double viewportFraction;
  final Clip clipBehavior;

  @override
  State<ITabBarView> createState() => _ITabBarViewState();
}

class _ITabBarViewState extends State<ITabBarView> {
  late Map<int, Widget?> pages = {};

  @override
  Widget build(BuildContext context) {
    if (widget.controller == null) {
      return const SizedBox.shrink();
    }
    return TabBarView(
        children: List.generate(widget.controller!.length, (index) {
      if (widget.controller!.index == index) {
        if (pages[index] == null) {
          final p = widget.pageBuilder(context, index);
          pages[index] = KeepAlivePage(child: p);
        }
        return pages[index]!;
      }

      return const SizedBox();
    }));
  }
}
